import requests
from comms.url_path import *


# 1.1登录鉴权
def login(username='seafile@admin.com', password='admin'):
    url = host + path_login_1
    header = {'content-type': 'application/x-www-form-urlencoded'}
    body = {'username': username, 'password': password}
    resp = requests.post(url, headers=header, data=body)
    print(f'获取响应状态码{resp.status_code}')
    print(f'获取响应头{resp.headers}')
    print(f'获取响应体{resp.text}')
    print(f'获取响应时间{resp.elapsed.total_seconds()}')
    return resp


# 1.2获取账号信息
def seafile_user_info(token):
    url = host + path_info_2
    header = {'Authorization': f'Token {token}'}

    resp = requests.get(url, headers=header)
    print(f'获取响应状态码{resp.status_code}')
    print(f'获取响应头{resp.headers}')
    print(f'获取响应体{resp.text}')
    print(f'获取响应时间{resp.elapsed.total_seconds()}')
    return resp


# 1.3新增资料库接口
def seafile_add_zlk(token, zlk_name):
    url = host + path_add_or_select_3_4
    header = {'Authorization': f'Token {token}'}
    body = {'name': zlk_name}

    resp = requests.post(url, headers=header, data=body)
    print(f'获取响应状态码{resp.status_code}')
    print(f'获取响应头{resp.headers}')
    print(f'获取响应体{resp.text}')
    print(f'获取响应时间{resp.elapsed.total_seconds()}')
    return resp


# 1.4查询所有的资料库接口
def seafile_zlk_select(token):
    url = host+path_add_or_select_3_4
    header = {'Authorization': f'Token {token}'}

    resp = requests.get(url, headers=header)
    print(f'获取响应状态码{resp.status_code}')
    print(f'获取响应头{resp.headers}')
    print(f'获取响应体{resp.text}')
    print(f'获取响应时间{resp.elapsed.total_seconds()}')
    return resp


# 1.5 修改资料库名称
def seafile_zlk_modify(repo_id, token, rname):
    url = get_path_5_6_7_8(repo_id, 5, )
    header = {'Authorization': f'Token {token}'}
    body = {'repo_name': rname}

    resp = requests.post(url, headers=header, data=body)
    print(f'获取响应状态码{resp.status_code}')
    print(f'获取响应头{resp.headers}')
    print(f'获取响应体{resp.text}')
    print(f'获取响应时间{resp.elapsed.total_seconds()}')
    return resp


# 1.6 删除指定的资料库接口
def seafile_zlk_delete(repo_id, token):
    url = get_path_5_6_7_8(repo_id, 6)
    header = {'Authorization': f'Token {token}'}

    resp = requests.delete(url, headers=header)
    print(f'获取响应状态码{resp.status_code}')
    print(f'获取响应头{resp.headers}')
    print(f'获取响应体{resp.text}')
    print(f'获取响应时间{resp.elapsed.total_seconds()}')
    return resp


# 1.7在指定的资料库目录新建文件接口
def sea_file_new_file(repo_id, file_name, token):
    url = get_path_5_6_7_8(repo_id, 7, file_name)
    header = {'Authorization': f'Token {token}'}
    body = {'operation': 'create'}

    resp = requests.post(url, headers=header, data=body)
    print(f'获取响应状态码{resp.status_code}')
    print(f'获取响应头{resp.headers}')
    print(f'获取响应体{resp.text}')
    print(f'获取响应时间{resp.elapsed.total_seconds()}')
    return resp


# 1.8在指定的资料库目录删除文件接口
def sea_file_delete_file(repo_id, file_name, token):
    url = get_path_5_6_7_8(repo_id, 8, file_name)
    header = {'Authorization': f'Token {token}'}

    resp = requests.delete(url, headers=header)
    print(f'获取响应状态码{resp.status_code}')
    print(f'获取响应头{resp.headers}')
    print(f'获取响应体{resp.text}')
    print(f'获取响应时间{resp.elapsed.total_seconds()}')
    return resp
